﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Admin_Hotjobs : System.Web.UI.Page
{
    protected string educationName;
    protected DataSet DsetPostedjobs = new DataSet();
    protected DataSet DsetHotJobs = new DataSet();
    protected System.Data.SqlClient.SqlDataReader Dreader;

   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillPostedjobs();
            JobpostingDetails.Visible = false;
            tdHotJobs.Visible = false;
            //BtnSaveHotJobs.Visible = false;
        }
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        obj.CommandText = "Proc_SaveHotJobs";
        System.Data.SqlClient.SqlParameterCollection obj1 = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters;
        string strid = MakepostingIdAsString();
        obj1.Add("@postingId", SqlDbType.NVarChar, 2000).Value = strid;
        obj.ExecuteNonQuery();
    }

    private string MakeHotJobsIdAsString()
    {
        string Id = "";
        if (DgridHotjobs.Rows.Count > 1)
        {
            for (int i = 0; i < DgridHotjobs.Rows.Count - 1; i++)
            {

                Control ctrl = DgridHotjobs.Rows[i].FindControl("ChkHotJobs");
                CheckBox chk = (CheckBox)ctrl;
                if (chk.Checked == false)
                {
                    Id = Id + Convert.ToString(DgridHotjobs.DataKeys[i].Value);
                    Id = Id + "|";
                }

            }
        }
        else
        {
            for (int i = 0; i <= DgridHotjobs.Rows.Count - 1; i++)
            {

                Control ctrl = DgridHotjobs.Rows[i].FindControl("ChkHotJobs");
                CheckBox chk = (CheckBox)ctrl;
                if (chk.Checked == false)
                {
                    Id = Id + Convert.ToString(DgridHotjobs.DataKeys[i].Value);
                    Id = Id + "|";
                }

            }
        }
        string strid="";
        if (Id.Length > 0)
        {
            strid = Id.Substring(0, Id.Length - 1);
        }
        //strunregistered = strunregistered.Substring(0, strunregistered.Length - 1);
        return strid;
    }

    private string MakepostingIdAsString()
    {
        string Id = "";

        for (int i = 0; i < DgridPostedJobs.Rows.Count - 1; i++)
        {

            Control ctrl = DgridPostedJobs.Rows[i].FindControl("ChkHotJobs");
            CheckBox chk = (CheckBox)ctrl;
            if (chk.Checked == true)
            {
                Id = Id + Convert.ToString(DgridPostedJobs.DataKeys[i].Value);
                Id = Id + ",1";
                Id = Id + "|";
            }
            else
            {
                Id = Id + Convert.ToString(DgridPostedJobs.DataKeys[i].Value);
                Id = Id + ",0";
                Id = Id + "|";
            }
           
        }
        string strid = Id.Substring(0, Id.Length - 1);
        //strunregistered = strunregistered.Substring(0, strunregistered.Length - 1);
        return strid;
    }

    private void FillPostedjobs()
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        DsetPostedjobs = obj.ExecuteDataSet("proc_GetJobPostingDetails");
        if (DsetPostedjobs.Tables.Count > 0)
        {
            if (DsetPostedjobs.Tables[0].Rows.Count > 0)
            {
                // msg.Visible = false;
                DgridPostedJobs.DataSource = DsetPostedjobs.Tables[0].DefaultView;
                DgridPostedJobs.DataBind();
                DgridPostedJobs.Visible = true;
                BtnSave.Visible = true;
            }
            else
            {
                //msg.Visible = true;
                DgridPostedJobs.Visible = false;
                BtnSave.Visible = false;
            }
        }
    }

    private void FillHotjobs()
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        DsetHotJobs = obj.ExecuteDataSet("Proc_GetHotJobsDetails");
        if (DsetHotJobs.Tables.Count > 0)
        {
            if (DsetHotJobs.Tables[0].Rows.Count > 0)
            {
                 msg.Visible = false;
                DgridHotjobs.DataSource = DsetHotJobs.Tables[0].DefaultView;
                DgridHotjobs.DataBind();
                DgridHotjobs.Visible = true;
                BtnSave.Visible = true;
            }
            else
            {
                msg.Visible = true;
                DgridHotjobs.Visible = false;
                BtnSave.Visible = false;
                BtnSaveHotJob.Visible = false;
            }
        }
    }

 
    
    protected void DgridPostedJobs_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Email")
        {
            string email = (string)e.CommandArgument;
            Response.Redirect("Adminemail.aspx?email=" + email);
        }
        else if (e.CommandName == "ViewDetails")
        {
            educationName = "";
            JobpostingDetails.Visible = true;
            string postingid = (string)e.CommandArgument;
            JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
            System.Data.SqlClient.SqlParameterCollection obj1 = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters;
            obj1.Add("@PostingId", SqlDbType.NVarChar, 2000).Value = postingid;
            //SqlParameter para = new SqlParameter("@educationlavelName", SqlDbType.NVarChar, 500);
            //para.Direction = ParameterDirection.Output;
            //obj1.Add(para);
            Dreader =(System.Data.SqlClient.SqlDataReader) obj.ExecuteReader("proc_GetJobPostingDetailsByID");
            if (Dreader.HasRows)
                Dreader.Read();

            //for getting name of educations

            educationName=Educationname(postingid);

        }
    }
    private string Educationname(string postingid)
    {
        SqlDataReader Dreader;
        string edName;
        edName = "";
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection obj1 = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters;
        obj1.Add("@PostingId", SqlDbType.NVarChar, 2000).Value = postingid;

        SqlParameter para = new SqlParameter("@EduName", SqlDbType.NVarChar, 500);
        para.Direction = ParameterDirection.Output;
        obj1.Add(para);
        //Dreader = (System.Data.SqlClient.SqlDataReader)obj.ExecuteReader("proc_GetEducationName");
        //if (Dreader.HasRows)
        //    Dreader.Read();
        //edName = para.Value.ToString();

        return edName;


        //DataSet dset = new DataSet();
        //JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        //System.Data.SqlClient.SqlParameterCollection obj1 = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters;
        //obj1.Add("@postingId", SqlDbType.NVarChar, 500).Value = postingid;
        //dset = obj.ExecuteDataSet("proc_GetEducationName");
        //if (dset.Tables.Count > 0)
        //{
        //    if (dset.Tables[0].Rows.Count > 0)
        //    {
        //        edName = dset.Tables[0].Rows[0][0].ToString();
        //    }
        //}
        //return edName;
       
    }
    protected void prevHotjobs_Click(object sender, EventArgs e)
    {
        if (prevHotjobs.Text == "Previous Hot jobs")
        {
            tdHotJobs.Visible = true;
            FillHotjobs();
            DgridPostedJobs.Visible = false;
            BtnSave.Visible = false;
            prevHotjobs.Text = "View All Jobs";
            JobpostingDetails.Visible = false;
        }
        else
        {
            tdHotJobs.Visible = false;
            DgridHotjobs.Visible = false;
            DgridPostedJobs.Visible = true;
            prevHotjobs.Text = "Previous Hot jobs";
            BtnSave.Visible = true;
            msg.Visible = false;
        }
    }
    protected void BtnSaveHotJob_Click(object sender, EventArgs e)
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        obj.CommandText = "Proc_RemoveHotJob";
        System.Data.SqlClient.SqlParameterCollection obj1 = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters;
        string strid = MakeHotJobsIdAsString();
        obj1.Add("@HotJobId", SqlDbType.NVarChar, 2000).Value = strid;
        obj.ExecuteNonQuery();
    }
    protected void DgridHotjobs_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName=="ContactPersonEmail")
        {
            string email = (string)e.CommandArgument;
            Response.Redirect("Adminemail.aspx?email=" + email); ;
        }
    }
  
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Admin/adminpostjobs.aspx");
    }
    protected void DgridPostedJobs_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Control ctrl = e.Row.FindControl("ChkHotJobs");
        if (ctrl != null)
        {
            CheckBox chk = (CheckBox)ctrl;

            if (DataBinder.Eval(e.Row.DataItem, "Hotstatus") != null)
            {
                string value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Hotstatus"));
                if (value == "1")
                {
                    chk.Checked = true;
                }
            }
            else
            {
                chk.Checked = false;
            }
        }
    }
    protected void DgridHotjobs_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DgridPostedJobs_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DgridPostedJobs.PageIndex = e.NewPageIndex;
    }
}
